package com.jmcgg.freesky.common.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.Logger;

/**
 * Title: FreeSky Common Java Library <br>
 * Description: FreeSky Common Java Library <br>
 * Copyright: Copyright (c) 2005~2009 www.afss.cn <br>
 * Company: Amei's FreeSky Studio <br>
 * 
 * @author <b>Jacky Chou</b> (AFSS [Amei's FreeSky Studio])
 * @version 1.3.0, 2009-02-09, Optimize the source code of this utility<br>
 *          1.2.1, 2008-08-05, Clean and optimize the source code.<br>
 *          1.2.0, 2008-02-23, Upgrade freesky.jar to 1.2.0 with Struts 2<br>
 *          1.0.0, 2006-05-30, Initial Version
 */
public class DateUtil extends FreeskyUtil {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(DateUtil.class);

	static Calendar calendar = null;

	public DateUtil() {
	}

	/**
	 * return current date with format 'yyyy-MM-dd'
	 * 
	 * @return yyyy-MM-dd
	 */
	public static String getDate() {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(date);
	}

	public static String getDateNoSpt() {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		return sdf.format(date);
	}

	/**
	 * yyyy-MM-dd HH:mm:ss
	 * 
	 * @return
	 */
	public static String getDateTime() {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(date);
	}

	public static String getDateTimeNoSpt() {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		return sdf.format(date);
	}

	public static String convert2Date(long milliSeconds) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date(milliSeconds);
		return sdf.format(date);
	}

	public static String convert2Time(long milliSeconds) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date time = new Date(milliSeconds);
		return sdf.format(time);
	}

	public static int getYear() {
		calendar = Calendar.getInstance();
		return calendar.get(Calendar.YEAR);
	}

	public static String getMonth() {
		calendar = Calendar.getInstance();
		int m = calendar.get(Calendar.MONTH);
		String[] months = new String[] { "January", "February", "March",
				"April", "May", "June", "July", "August", "September",
				"October", "November", "December" };
		if (m > 11) {
			return "Unknown to Man";
		}
		return months[m];
	}

	public static String getDay() {
		calendar = Calendar.getInstance();
		int x = calendar.get(Calendar.DAY_OF_WEEK);
		String[] days = new String[] { "Sunday", "Monday", "Tuesday",
				"Wednesday", "Thursday", "Friday", "Saturday" };
		if (x > 6) {
			return "Unknown to Man";
		}
		return days[x];
	}

	/**
	 * 得到月份的数字表示
	 * 
	 * @return 月份的数字表示
	 */
	public static int getMonthInt() {
		return 1 + calendar.get(Calendar.MONTH);
	}

	public static int getDayOfMonth() {
		return calendar.get(Calendar.DAY_OF_MONTH);
	}

	public static int getDayOfYear() {
		return calendar.get(Calendar.DAY_OF_YEAR);
	}

	public static int getWeekOfYear() {
		return calendar.get(Calendar.WEEK_OF_YEAR);
	}

	public static int getWeekOfMonth() {
		return calendar.get(Calendar.WEEK_OF_MONTH);
	}

	public static int getDayOfWeek() {
		return calendar.get(Calendar.DAY_OF_WEEK);
	}

	public static int getHour() {
		return calendar.get(Calendar.HOUR_OF_DAY);
	}

	public static int getMinute() {
		return calendar.get(Calendar.MINUTE);
	}

	public static int getSecond() {
		return calendar.get(Calendar.SECOND);
	}

	public static int getEra() {
		return calendar.get(Calendar.ERA);
	}

	public String getUSTimeZone() {
		String[] zones = new String[] { "Hawaii", "Alaskan", "Pacific",
				"Mountain", "Central", "Eastern" };

		return zones[10 + getZoneOffset()];
	}

	public static int getZoneOffset() {
		return calendar.get(Calendar.ZONE_OFFSET) / (60 * 60 * 1000);
	}

	public static int getDSTOffset() {
		return calendar.get(Calendar.DST_OFFSET) / (60 * 60 * 1000);
	}

	public static int getAMPM() {
		return calendar.get(Calendar.AM_PM);
	}

	/**
	 * 把日期的格式转换成时间的格式 YYYY-MM-DD ==> YYYY-MM-DD HH:MM:SS
	 * 
	 * @param date
	 * @return
	 */
	public static String date2Time(String date) {
		if (date == null || date.length() == 0) {
			return null;
		}
		String tempTime = new String(date);
		tempTime = tempTime + " 00:00:00";
		return tempTime;
	}

	/**
	 * 把日期的格式转换成时间的格式（而且是后一个时间，需要加上23：59：59 YYYY-MM-DD ==> YYYY-MM-DD HH:MM:SS
	 * 
	 * @param date
	 * @param isEndTime
	 *            1:表示是后一个时期
	 * @return
	 */
	public static String date2Time(String date, int isEndTime) {
		if (date == null || date.length() < 1) {
			return null;
		}
		String tempTime = new String(date);
		if (isEndTime == 1) {
			tempTime = tempTime + " 23:59:59";
		}
		return tempTime;
	}

	/**
	 * 计算两个日期之间相差的月数: 满一个月算一个月，不满一个月不算
	 * 
	 * @param smallDate
	 * @param bigDate
	 * @return
	 */
	public static int getMonthNum(String smallDate, String bigDate) {
		if (smallDate == null || smallDate.length() < 1 || bigDate == null
				|| bigDate.length() < 1) {
			return -1;
		}
		// if(smallDate > bigDate) return -1;
		String[] strDate1 = smallDate.split("-");
		String[] strDate2 = bigDate.split("-");
		int monthNum = 0;
		try {
			monthNum = (Integer.parseInt(strDate2[0]) - Integer
					.parseInt(strDate1[0]))
					* 12
					+ Integer.parseInt(strDate2[1])
					- Integer.parseInt(strDate1[1]);
			if (lastDateOfMonth(bigDate)) {
				return monthNum;
			}
			if (Integer.parseInt(strDate2[2]) - Integer.parseInt(strDate1[2]) >= 0) {
				return monthNum;
			}
			return monthNum - 1;
		} catch (NumberFormatException nfe) {
			logger.error("getMonthNum(String, String) - smallDate=" + smallDate
					+ ", bigDate=" + bigDate, nfe);
			return -1;
		}
	}

	/**
	 * 判断是否是一个月的最后一天
	 * 
	 * @param date
	 * @return
	 */
	public static boolean lastDateOfMonth(String date) {
		String[] strDate = date.split("-");
		int[] iDate = new int[3];
		try {
			for (int i = 0; i < strDate.length; i++) {
				iDate[i] = TypeUtil.str2Int(strDate[i]);
			}
			switch (iDate[1]) {
			case 1: // 判断大月31天
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				if (iDate[2] == 31) {
					return true;
				}
				break;
			case 4: // 判断小月30天
			case 6:
			case 9:
			case 11:
				if (iDate[2] == 30) {
					return true;
				}
				break;
			case 2: // 判断是否2月
				if (iDate[2] == 29) {
					return true;
				}
				if (iDate[2] == 28) {
					if (!isLeapYear(getYear())) {
						return true;
					}
				}
				break;
			default:
				return false;
			}
		} catch (NumberFormatException nfe) {
			logger.error("lastDateOfMonth(String) - String date=" + date, nfe);
		}
		return false;
	}

	/**
	 * 判断某年是否是闰年
	 * 
	 * @param year
	 * @return boolean
	 */
	public static boolean isLeapYear(int year) {
		if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 得到下个月的1日
	 * 
	 * @param
	 * @return date
	 */
	public static String getFDayOfNM() {
		calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);

		String monthStr = "0";
		if (month == 11) {
			month = 0;
			year += 1;
		} else {
			month++;
		}
		if (month < 9) {
			monthStr += month;
		} else {
			monthStr = String.valueOf(month);
		}
		return String.valueOf(year) + "-" + monthStr + "-" + "01";
	}
}
